package com.yisu.auction.api.core.service.impl;

import com.alibaba.fastjson.JSON;
import com.yisu.auction.api.core.mapper.*;
import com.yisu.auction.api.core.service.BaseService;
import com.yisu.auction.api.core.service.IRechargeService;
import com.yisu.auction.api.core.service.impl.func.BalanceLogUtil;
import com.yisu.auction.api.domain.request.memberPackage.MemberRequest;
import com.yisu.auction.api.domain.request.rechargePackage.RechargeRequest;
import com.yisu.auction.api.domain.vo.activityPackageVos.NewUserRechargeVo;
import com.yisu.auction.ewf.entity.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * author: 赵杰
 * Business Description：充值相关业务接口实现
 * createTime：2017/8/19
 */
@Service("IRechargeService")
public class RechargeServiceImpl extends BalanceLogUtil implements IRechargeService {
    @Resource
    private RechargeMapper rechargeMapper;
    @Resource
    private MemberMapper memberMapper;
    @Resource
    private MobileRechargeMapper mobileRechargeMapper;
    @Resource
    private PrizeArchiveMapper prizeArchiveMapper;
    @Resource
    private PrizeMapper prizeMapper;

    /**
     * author: 赵杰
     * Business Description：充值相关业务接口
     *
     * @param rechargeRequest 含金额，用户memberId
     * @return
     */
    @Override
    public String createRechargeOrder(RechargeRequest rechargeRequest) throws InterruptedException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");//临时编号
        Thread.sleep(1000);
        String orderId = rechargeRequest.getOrderSign() + simpleDateFormat.format(new Date());
        Recharge insertModel = new Recharge();
        insertModel.setCreateDate(new Date());
        insertModel.setIsValid(true);
        insertModel.setType(rechargeRequest.getPayType());
        insertModel.setMoney(new BigDecimal(rechargeRequest.getAmount()));
        insertModel.setSysType(rechargeRequest.getAppType());
        insertModel.setStatus(0);
        insertModel.setOrderNo(orderId);
        insertModel.setMemberId(Long.valueOf(rechargeRequest.getMemberId()));
        insertModel.setObjDesc(rechargeRequest.getDescription());
        if (rechargeMapper.insertSelective(insertModel) > 0) {
            return orderId;
        } else {
            return "";
        }
    }


    /**
     * author: 赵杰
     * Business Description：支付通知接口
     *
     * @param rechargeRequest
     * @return
     */
    @Override
    public boolean notifyResult(RechargeRequest rechargeRequest) {
        Recharge recharge = rechargeMapper.selectByOrderId(rechargeRequest);
        if (recharge != null) {
            if (recharge.getStatus() == 1) {
                return true;
            }
            Member member = memberMapper.selectByPrimaryKey(Long.valueOf(recharge.getMemberId()));
            if (member == null)
                return false;
            rechargeMapper.updateRechargeStatus(recharge.getId());
            if (recharge.getOrderNo().toUpperCase().contains(MobileRecharge.RechargeTypeEnum.Balance.name)
                    ||recharge.getOrderNo().toUpperCase().contains(MobileRecharge.RechargeTypeEnum.Traffic.name)) {
                //话费流量充值
                MobileRecharge mobileRecharge = mobileRechargeMapper.getMobileRechargeByOrderId(recharge.getOrderNo());
                if (mobileRecharge == null)
                    throw new RuntimeException("话费充值流量异常");
                MobileRecharge updateMobileRecharge = new MobileRecharge();
                updateMobileRecharge.setId(mobileRecharge.getId());
                updateMobileRecharge.setStatus(2);
                return mobileRechargeMapper.updateByPrimaryKeySelective(updateMobileRecharge) > 0;
            }
            if (recharge.getOrderNo().toLowerCase().contains("pricespreadbuy")) {
                //差价购买逻辑--后变更为去付款逻辑
                long prizeId = Long.parseLong(recharge.getObjDesc());
                Prize prize = prizeMapper.selectByPrimaryKey(prizeId);
                Prize updateModel = new Prize();
                updateModel.setId(prizeId);
                updateModel.setOrderStatus(1);//已付款，待发货
                updateModel.setPaymentTime(new Date());
                if (prize == null) {
                    return prizeArchiveMapper.updateByPrimaryKeySelective(updateModel) > 0;
                } else {
                    return prizeMapper.updateByPrimaryKeySelective(updateModel) > 0;
                }
            }
            if (recharge.getOrderNo().toLowerCase().contains("newuserrecharge")) {
                NewUserRechargeVo newUserRechargeVo = JSON.parseObject(recharge.getObjDesc(), NewUserRechargeVo.class);
                if (newUserRechargeVo == null)
                    return false;
                rechargeCommon(member, recharge.getMoney(), 1, "【用户首冲】");
                rechargeCommon(member, new BigDecimal(newUserRechargeVo.getGivePresent()), 2, "【首冲赠拍币】");
                return true;
            } else {
                rechargeCommon(member, recharge.getMoney(), 1, "【用户充值】");
                return true;
            }
        }
        return false;
    }

    /**
     * 充值方法公用提取
     *
     * @param member
     * @param operateNum
     * @param walletType
     * @param desc
     */
    protected void rechargeCommon(Member member, BigDecimal operateNum, Integer walletType, String desc) {
        addMemberWallet(member, operateNum, 0, walletType, desc);
    }


    /**
     * author: 赵杰
     * Business Description：通过订单号获取充值订单信息
     *
     * @param orderId
     * @return
     */
    @Override
    public Recharge getRechargeOrderByOrderId(String orderId) {
        return rechargeMapper.getRechargeOrderByOrderId(orderId);
    }

}
